import {
  defineStore
} from 'pinia'
import {
  ref
} from 'vue'
import {
  getPlaylistReviewAPI
} from '@/common/api/commentAPI.js'

export const useCommontStore = defineStore('commont', () => {
  const playlistReview = ref({})
  const reviewCommonts = ref([])
  const reviewHotCommonts = ref([])
  
  // 获取歌单评论
  const getPlaylistReview = async (id, limit, offset, before) => {
    if(offset === 0){
      reviewCommonts.value = []
      reviewHotCommonts.value = []
    }
    const res = await getPlaylistReviewAPI(id, limit, offset, before)
    if (res.code !== 200) return uni.showToast({
      title: '获取评论失败！',
      duration: 2000,
      icon: 'error'
    })
    playlistReview.value = res
    reviewCommonts.value.push(...res.comments)
    if(res.hotComments){
      reviewHotCommonts.value.push(...res.hotComments)
    }
  }
  
  return {
    playlistReview,
    reviewCommonts,
    reviewHotCommonts,
    getPlaylistReview
  }
})